package entity

import (
	"fmt"
	"time"

	"shqsoft.cn/pasnj/base"
	"shqsoft.cn/pasnj/workflow"
	"shqsoft.cn/pasnj/workflow/entity"

	"github.com/jinzhu/gorm"
)

//对公 新客户 认定 审批
type CompClieDiviFlowView struct {
	ID         int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	ENCD       string    `gorm:"column:ENCD"`
	CUNM       string    `gorm:"column:CUNM"`
	ITNM       string    `gorm:"column:ITNM"`
	OrganName  string    `gorm:"column:OrganName"`
	CUID       int       `gorm:"column:CUID"`
	ACNO       string    `gorm:"column:ACNO"`
	BANO       string    `gorm:"column:BANO"`
	OPDT       time.Time `gorm:"column:OPDT"`
	YWLX       string    `gorm:"column:YWLX"`
	ACID       int       `gorm:"column:ACID"`
	DSDT       time.Time `gorm:"column:DSDT"`
	EMNO1      string    `gorm:"column:EMNO1"`
	EMID1      int       `gorm:"column:EMID1"`
	EMNM1      string    `gorm:"column:ENNM1" json:"emnm1"`
	DSVL1      float64   `gorm:"column:DSVL1"`
	EMNO2      string    `gorm:"column:EMNO2"`
	EMNM2      string    `gorm:"column:ENNM2" json:"emnm2"`
	EMID2      int       `gorm:"column:EMID2"`
	DSVL2      float64   `gorm:"column:DSVL2"`
	EMNO3      string    `gorm:"column:EMNO3"`
	EMNM3      string    `gorm:"column:ENNM3" json:"emnm3"`
	EMID3      int       `gorm:"column:EMID3"`
	DSVL3      float64   `gorm:"column:DSVL3"`
	EMNO4      string    `gorm:"column:EMNO4"`
	EMNM4      string    `gorm:"column:ENNM4" json:"emnm4"`
	EMID4      int       `gorm:"column:EMID4"`
	DSVL4      float64   `gorm:"column:DSVL4"`
	EMNO5      string    `gorm:"column:EMNO5"`
	EMNM5      string    `gorm:"column:ENNM5" json:"emnm5"`
	EMID5      int       `gorm:"column:EMID5"`
	DSVL5      float64   `gorm:"column:DSVL5"`
	EMNO6      string    `gorm:"column:EMNO6"`
	EMNM6      string    `gorm:"column:ENNM6" json:"emnm6"`
	EMID6      int       `gorm:"column:EMID6"`
	DSVL6      float64   `gorm:"column:DSVL6"`
	UID        int       `gorm:"column:UID"`
	FRID       int       `gorm:"column:FRID"`
	THSM       string    `gorm:"column:THSM"`
	SQR        string    `gorm:"column:SQR"`
	STEP       int       `gorm:"column:STEP" json:"step"`
	Progress   float64   `gorm:"column:Progress" json:"progress"`
	StepName   string    `gorm:"-" json:"stepName"`
	Status     string    `gorm:"column:STATE" json:"status"`
	CREATETIME time.Time `gorm:"column:CREATETIME"`
	UPDATETIME string    `gorm:"column:UPDATETIME"`
}

//TableName User用户表名
func (CompClieDiviFlowView) TableName() string {
	return "V_COMP_CLIE_DIVI_View"
}

func CompClieDiviFlowViewEF() base.EntityFunc {
	return func() interface{} {
		entity := CompClieDiviFlowView{}
		return &entity
	}
}
func CompClieDiviFlowViewESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []CompClieDiviFlowView
		return &entities
	}
}

// // AfterFind 查询后触发器
func (i *CompClieDiviFlowView) AfterFind(tx *gorm.DB) (err error) {
	var emid1, emid2, emid3, emid4, emid5 int
	sqlEx := "select ID from T_SysUser where SysUserID=?"
	row := tx.Raw(sqlEx, i.EMNO1).Row()
	row.Scan(&emid1)
	if emid1 == 0 {
		emid1 = -1
		i.EMNO1 = ""
		i.EMNM1 = ""
	}
	i.EMID1 = emid1

	sqlEx = "select ID from T_SysUser where SysUserID=?"
	row = tx.Raw(sqlEx, i.EMNO2).Row()
	row.Scan(&emid2)
	if emid2 == 0 {
		emid2 = -1
		i.EMNO2 = ""
		i.EMNM2 = ""
	}
	i.EMID2 = emid2

	sqlEx = "select ID from T_SysUser where SysUserID=?"
	row = tx.Raw(sqlEx, i.EMNO3).Row()
	row.Scan(&emid3)
	if emid3 == 0 {
		emid3 = -1
		i.EMNO3 = ""
		i.EMNM3 = ""
	}
	i.EMID3 = emid3

	sqlEx = "select ID from T_SysUser where SysUserID=?"
	row = tx.Raw(sqlEx, i.EMNO4).Row()
	row.Scan(&emid4)
	if emid4 == 0 {
		emid4 = -1
		i.EMNO4 = ""
		i.EMNM4 = ""
	}
	i.EMID4 = emid4

	sqlEx = "select ID from T_SysUser where SysUserID=?"
	row = tx.Raw(sqlEx, i.EMNO1).Row()
	row.Scan(&emid1)
	if emid5 == 0 {
		emid5 = -1
		i.EMNO5 = ""
		i.EMNM5 = ""
	}
	i.EMID5 = emid5

	if i.FRID == 0 {
		i.Status = "exception"
		i.StepName = "校验错误"
		return
	}
	run := entity.FlowRun{}
	if err = tx.Find(&run, i.FRID).Error; err != nil {
		tx.Rollback()
		return
	}
	wf := workflow.GetWorkFlow(run.WFID)
	if wf == nil {
		return fmt.Errorf("指定的流程ID:%d在流程配置中不存在！", run.WFID)
	}
	act := wf.GetAction(run.Step)
	if act == nil {
		return fmt.Errorf("指定的stepID:%d在流程%s配置中不存在！", run.Step, wf.Name)
	}
	count := len(wf.Actions)
	i.StepName = "审批中"
	if i.STEP == 1 {
		i.StepName = "申请中"
	}
	i.Progress = float64(act.Step*100) / float64(count)
	if i.Progress >= 100 && run.State == "0" {
		i.Progress = 95
	}
	i.Status = ""
	if run.State == "0" {
		i.Status = "active"
	} else if run.State == "2" {
		i.StepName = "已中止"
		i.Status = "exception"
	} else {
		i.StepName = "已完成"
		i.Status = "success"
	}
	return
}

func init() {
	dao := base.NewDefaultDao(CompClieDiviFlowViewEF(), CompClieDiviFlowViewESF(), nil)
	filter := base.WorkFolowFilter{
		FilterColName: "FRID",
	}
	filter.SetAdditional("wfid", "3")
	dao.Fileter = &filter
	base.DaoPoolObj.Put("CompClieDiviFlowView", dao)
}
